using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

using SqlWebAdmin.CSharp;

public partial class Columns : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
                lblError.Visible = false;
                lblNameProperty.Text = Request.QueryString["table"];

                Server smoServer = SmoHelper.GetServer();

				try
				{

					string databaseName = Request.QueryString["database"];
					Database smoDatabase = smoServer.Databases[databaseName];

					if (smoDatabase != null)
					{
						string tableName = Request.QueryString["table"];
						string tableSchema = Request.QueryString["schema"];

						Microsoft.SqlServer.Management.Smo.Table smoTable = smoDatabase.Tables[tableName, tableSchema];

						if (smoTable == null)
						{
							Response.Redirect(String.Format("error.aspx?error={0}", Globals.tableNotFoundErrorNumber), false);
						}
						else
						{

							dgColumns.DataSource = smoTable.Columns;
							dgColumns.DataBind();

							if (smoTable.RowCount > 0)
							{
								dgColumns.Columns[2].Visible = true;
								dgColumns.Columns[3].Visible = false;
								dgColumns.Columns[8].Visible = false;
							}
							else
							{
								dgColumns.Columns[2].Visible = false;
								dgColumns.Columns[3].Visible = true;
								dgColumns.Columns[8].Visible = true;
							}
							// A table must have at least 1 column
							if (smoTable.Columns.Count == 1)
							{
								dgColumns.Columns[9].Visible = false;
							}
							else
							{
								dgColumns.Columns[9].Visible = true;
							}
							if (smoTable.Columns.Count > 0)
							{
								dgColumns.Visible = true;
							}
							else
							{
								lblNoColumns.Visible = true;
							}

						}

					}
					else
					{
						Response.Redirect(String.Format("error.aspx?error={0}", Globals.tableNotFoundErrorNumber), false);
					}
				}
				catch (SmoException smoEx)
				{
					lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx));
					lblError.Visible = true;
				}
				catch (System.Exception ex)
				{
					lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex));
					lblError.Visible = true;
				}
				finally
				{
					smoServer.ConnectionContext.Disconnect();
				}
            }
         
    }

    protected void btnAddNewColumn_Click(object sender, EventArgs e)
    {
        Response.Redirect("EditColumn.aspx?Database=" + HttpUtility.UrlEncode(Request.QueryString["database"]) + "&table=" + HttpUtility.UrlEncode(Request.QueryString["table"]) + "&schema=" + HttpUtility.UrlEncode(Request.QueryString["schema"]) + "&column=" + HttpUtility.UrlEncode(Request.QueryString["column"]));

    }
}
